<template>
  <div v-if="system" class="system-detail-page-wrapper">
    <div :style="{ margin: '10px 0 15px 0', display: 'flex', alignItems: 'center' }">
      <div class="el-descriptions__title">{{ system.name }}</div>
      <el-text v-if="system.description" class="w-150px mb-2" truncated :style="{ marginLeft: '15px' }">
        {{ system.description }}
      </el-text>
    </div>
    <el-container :style="{ height: 'calc(100vh - 106px)' }">
      <el-aside width="150px">
        <el-menu :default-active="defaultActive" :style="{ height: '100%' }">
          <el-menu-item index="SystemMetrics" @click="onClickMenu">
            <el-icon><Odometer /></el-icon>
            <span>系统指标</span>
          </el-menu-item>
          <el-sub-menu index="1">
            <template #title>
              <el-icon><Menu /></el-icon>
              <span>系统架构</span>
            </template>
            <el-menu-item index="FoundationArchitecture" @click="onClickMenu">底座清单</el-menu-item>
            <el-menu-item index="SystemArchitecture" @click="onClickMenu">当前架构</el-menu-item>
            <el-menu-item index="TargetArchitecture" @click="onClickMenu">目标架构</el-menu-item>
          </el-sub-menu>
          <el-menu-item index="Applications" @click="onClickMenu">
            <el-icon><Cpu /></el-icon>
            <span>应用管理</span>
          </el-menu-item>
          <el-sub-menu index="2">
            <template #title>
              <el-icon><View /></el-icon>
              <span>访问控制</span>
            </template>
            <el-menu-item index="ApiPublish" @click="onClickMenu">API 发布</el-menu-item>
            <el-menu-item index="ApiSubscribe" @click="onClickMenu">API 订阅</el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-aside>
      <el-main><router-view /></el-main>
    </el-container>
  </div>
</template>

<script lang="ts" setup>
import { useSystemService } from '@/service/system-service';
import { System } from '@/store/system-repository';
import { Cpu, View, Menu, Odometer } from '@element-plus/icons-vue';
import { MenuItemRegistered } from 'element-plus';

const router = useRouter();
const route = useRoute();
const systemService = useSystemService();

const onClickMenu = (item: MenuItemRegistered) => {
  router.push({ name: item.index });
};

const MENUS = new Set(['SystemMetrics', 'SystemArchitecture', 'TargetArchitecture', 'Applications', 'AccessControl']);
const getDefaultActive = () => {
  const matched_ = route.matched;
  for (let i = 0; i < matched_.length; i++) {
    const name = matched_[i].name as string;
    if (MENUS.has(name)) {
      return name;
    }
  }
  return 'Workbench';
};
const defaultActive = getDefaultActive();

const system = ref<System>();
watch(
  () => {
    return route.params.systemId as string | undefined;
  },
  newSystemId => {
    if (newSystemId) {
      systemService.system(newSystemId).then(data => {
        system.value = data;
      });
    }
  },
  { immediate: true },
);
</script>

<style lang="less" scoped>
.system-detail-page-wrapper {
  :deep(.el-main) {
    padding: 15px;
  }
}
</style>
